<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="900" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="项目编号" prop="project_id">
                        <el-input v-model="project_code" readonly placeholder="请选择项目" @click="checkItem"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="会议名称" prop="project_name">
                        <el-input v-model="project_name" disabled placeholder="会议名称"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="申请人" prop="apply_people">
                    <el-select v-model="form.apply_people" filterable clearable placeholder="请选择销售" style="width:100%">
                        <el-option v-for="item in peopleList" :key="item.id" :label="item.name" :value="item.id"/>
                    </el-select>
                </el-form-item>
            </el-col>
            <!-- <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="申请人" prop="apply_people">
                        <el-select v-model="form.apply_people" filterable clearable placeholder="请选择销售" style="width:100%">
                            <el-option v-for="item in peopleList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="申请金额" prop="apply_money">
                        <el-input v-model="form.apply_money" placeholder="申请金额"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
             <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="已核销金额" prop="verification_money">
                        <el-input v-model="form.verification_money" readonly placeholder="已核销金额" @click="checkReferrer"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="未核销金额" prop="not_verification_money">
                        <el-input v-model="form.not_verification_money" readonly placeholder="未核销金额"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="核销凭证" prop="verification_voucher">
                        <sc-upload-multiple width='50' height='50' v-model="form.verification_voucher" draggable></sc-upload-multiple>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="申请事由" prop="apply_reason">
                        <el-input v-model="form.apply_reason" placeholder="申请事由" type="textarea"></el-input>
                    </el-form-item>
                </el-col>
            </el-row> -->
            <el-form-item label="申请明细">
                <sc-form-table ref="table" class="formtable" v-model="form.detail" :addTemplate="detail" placeholder="暂无数据" height="300" show-summary>
                    <el-table-column label="申请事由" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.reason" placeholder="申请事由"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="金额" align="center" prop="price">
                        <template #default="scope">
                            <el-input v-model="scope.row.price" placeholder="金额" @input="input"></el-input>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="申请时间" prop="apply_time">
                        <el-date-picker v-model="form.apply_time" type="date" placeholder="申请时间" style="width: 100%;" :value-format="'YYYY-MM-DD'" disabled></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="申请状态" prop="apply_status">
                        <el-select v-model="form.apply_status" placeholder="请选择申请状态" style="width:100%" disabled>
                            <el-option label="已通过" :value="1"/>
                            <el-option label="未通过" :value="0"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <!-- <el-col :span="12">
                <el-form-item label="核销状态" prop="verification_status">
                    <el-select v-model="form.verification_status" placeholder="请选择核销状态" style="width:100%" clearable filterable>
                        <el-option label="未核销" :value="0"/>
                        <el-option label="核销完成" :value="1"/>
                    </el-select>
                </el-form-item>
            </el-col> -->
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <item-list v-if="dialog.item" ref="itemDialog" @success="itemBack" @closed="dialog.item=false"></item-list>
</template>

<script>
import itemList from '../list/itemList.vue';
export default {
    components: { itemList},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增备用金',
				edit: '编辑备用金',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                reserve_code:"",
                project_id:"",
                apply_reason:"",
                apply_money:0,
                verification_money:0,
                not_verification_money:0,
                verification_voucher:"",
                apply_people:this.$TOOL.data.get("USER_INFO").admin_id,
                apply_time:this.moment().format('YYYY-MM-DD'),
                apply_status:0,
                verification_status:"",
                detail:[],
			},
            project_code: "",
            project_name: "",
			//验证规则
			rules: {
				enterprise: [{ required: true, message: '请输入名称' }],
			},
            peopleList:[],
            dialog:{
                item:false,
            },
            detail:{},
		}
	},
	mounted() {
        this.getUser()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
            console.log('this.form :>> ', this.form);
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.project.Imprest.edit.post(this.form);
					} else {
						res = await this.$API.project.Imprest.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        projectData(data){
            this.form.project_id = data.project_id
            this.project_code = data.project_code
            this.project_name = data.project_name
        },
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            var res = await this.$API.project.Imprest.detail.get({id:data.id});
            this.form.project_id = res.data.project_id
            this.form.reserve_code = res.data.reserve_code
            this.project_code = res.data.project.project_code
            this.project_name = res.data.project.metting
            this.form.apply_reason = res.data.apply_reason
            this.form.apply_money = res.data.apply_money
            this.form.verification_money = res.data.verification_money
            this.form.not_verification_money = res.data.not_verification_money
            this.form.verification_voucher = res.data.verification_voucher
            this.form.apply_people = Number(res.data.apply_people)
            this.form.apply_time = res.data.apply_time
            this.form.apply_status = res.data.apply_status
            this.form.verification_status = res.data.verification_status
            this.form.detail = data.detail ? data.detail : []
		},
        async getUser(){
            var res = await this.$API.system.admin.all.get();
            this.peopleList = res.data;
        },
        checkItem(){
            this.dialog.item = true
			this.$nextTick(() => {
				this.$refs.itemDialog.open('add')
			})
        },
        itemBack(data){
            this.form.project_id = data.id
            this.project_code = data.project_code
            this.project_name = data.metting
        },
        input(){
            let count = 0
            this.form.detail.map(item=>{
                if(item.price){
                    count = count + Number(item.price)
                }
            })
            this.form.apply_money = count
        },
	}
}
</script>

<style></style>
